# $Id: makefile,v 1.32 2004/09/28 17:27:12 pcharles Exp $
#
#   package net.sourceforge.jpcap.capture
#
PKG = net.sourceforge.jpcap.capture
PKG_DIR = $(subst .,/,$(PKG))
PKG_UND = $(subst .,_,$(PKG))

include ${MAKE_HOME}/os.makefile
# include ${MAKE_HOME}/rules.makefile

WRAPPER = PacketCapture

# on linux, OSTYPE is Linux and the include subdir is called 'linux'
# on solaris, they match. need to lower case for other os variants?
OTL = $(subst L,l,${OSTYPE})

# on debian linux, make 'linux-gnu' recognizable as linux (.. apologies to RMS)
ifeq ($(OTL), linux-gnu)
	OTL=linux
endif


## set library name, paths and options for each supported platform group

# All Unix variants, including: Solaris, Linux, AIX, etc..
ifeq ($(OSFLAVOR), UNIX)
	SO_TARGET = libjpcap.so
	INCLUDES = \
		-I/usr/include/pcap \
		-I${JAVA_HOME}/include \
		-I${JAVA_HOME}/include/$(OTL)
	LIBS = -lnsl /usr/local/lib/libpcap.a
ifneq ($(LIB_PCAP),)
	LIBS = -lnsl ${LIB_PCAP}
endif 
else
# Mac OS X
ifeq ($(OSFLAVOR), DARWIN)
	SO_TARGET = libjpcap.jnilib
	INCLUDES = -I/usr/include/pcap \
		   -I${JAVA_HOME}/include \
		   -I${JAVA_HOME}/include/darwin \
                   -I$(SDKROOT)/System/Library/Frameworks/JavaVM.framework/Headers
    LIBS = /usr/local/lib/libpcap.A.dylib
ifneq ($(LIB_PCAP),)
	LIBS = ${LIB_PCAP}
endif 

else
# Windows, using MinGW
ifeq ($(OSFLAVOR), MINGW)
	SO_TARGET = jpcap.dll
	INCLUDES = \
		-I${WINPCAP_HOME}/Include/ \
		-I${JAVA_HOME}/include \
		-I${JAVA_HOME}/include/win32 \
		-I.
	LIBS = -L ${WINPCAP_HOME}/Lib -lwpcap
else
#
# Cygwin and native DOS-mode builds aren't supported due to lack of interest 
# from the community. If you'd like to change that, submit patches at 
# http://www.sf.net/projects/jpcap.
#
# ifeq ($(OSFLAVOR), CYGWIN)
# ifeq ($(OSFLAVOR), DOS)
#
endif
endif
endif


all: $(SO_TARGET)


CCOPTS = -Wall -O2

# build the native component
# NOTE: the -fPIC option below is only required on Solaris?
ifeq ($(OSFLAVOR), UNIX)
$(SO_TARGET): $(PKG_UND)_$(WRAPPER).h jpcap.c process.cpp
	g++ $(CCOPTS) $(INCLUDES) jpcap.c process.cpp -fPIC -shared -o $@ $(LIBS)
else
ifeq ($(OSFLAVOR), DARWIN)
$(SO_TARGET): $(PKG_UND)_$(WRAPPER).h jpcap.c process.cpp
	g++ $(CCOPTS) $(INCLUDES) jpcap.c process.cpp -bundle -o $@ $(LIBS)
else
$(SO_TARGET): $(PKG_UND)_$(WRAPPER).h jpcap.c
	g++ $(CCOPTS) -D_JNI_IMPLEMENTATION $(INCLUDES) -Wl,--kill-at \
	jpcap.c process.cpp -shared -o $@ $(LIBS)

endif
endif

$(PKG_UND)_$(WRAPPER).h: ${PROJECT_HOME}/src/main/java/$(PKG_DIR)/$(WRAPPER).java
#javac ${PROJECT_HOME}/src/java/$(PKG_DIR)/$(WRAPPER).java
ifeq ($(OSFLAVOR), DARWIN)
	javah -jni -cp ${CLASSPATH} $(PKG).$(WRAPPER)
else
	javah -jni -cp ${CLASSPATH} $(PKG).$(WRAPPER)
endif
#	javap $(PKG).$(WRAPPER)


# NOTE: to run the class PacketCapture, the path to the shared object
# must be in the library path.
# On Linux, this means LD_LIBRARY_PATH.must point to the path of libjpcap.so.
# On Windows, this means that the path containing jpcap.dll must be in PATH.
# On Mac OS X, DYLD_LIBRARY_PATH must point the path of libjpcap.jnilib.

# 用maven测试
#LIB_PATH = ${PROJECT_HOME}/src/java/$(PKG_DIR)
#test: all
#	export LD_LIBRARY_PATH=$(LIB_PATH) ; java $(PKG).CaptureTest


#include ${MAKE_HOME}/targets.makefile
#include ${MAKE_HOME}/depend.makefile


# override default clean target to remove stuff generated by the jni compiler
clean:
	rm -fv *.o
	rm -fv *.so
	rm -fv jpcap
	rm -fv $(PKG_UND)_$(WRAPPER).h
	rm -fv *~
	rm -fv *.class
	rm -fv *.dll
	rm -fv *.exp
	rm -fv *.a
	rm -fv *.jnilib
